package com.dayouzi.crawler_monitor.service.impl;

import com.dayouzi.crawler_monitor.base.exception.BusinessException;
import com.dayouzi.crawler_monitor.mapper.PersonMapper;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.query.PersonConfigListQuery;
import com.dayouzi.crawler_monitor.pojo.query.PersonConfigQuery;
import com.dayouzi.crawler_monitor.pojo.query.PersonListQuery;
import com.dayouzi.crawler_monitor.pojo.vo.HonorListVO;
import com.dayouzi.crawler_monitor.pojo.vo.PersonConfigListVO;
import com.dayouzi.crawler_monitor.pojo.vo.PersonListVO;
import com.dayouzi.crawler_monitor.service.PersonService;
import com.dayouzi.crawler_monitor.utils.DateUtils;
import com.dayouzi.crawler_monitor.utils.ScheduleUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public PageBean<PersonListVO> listPage(Integer page, Integer limit, PersonListQuery personListQuery) {
        // 分页查询
        PageHelper.startPage(page, limit);
        List<PersonListVO> list = personMapper.listPage(personListQuery);

        // 处理日期间隔时间
        for (PersonListVO personListVO : list) {
            // 计算最近一次运行时间与当前时间相差多少天
            String logTime = personListVO.getLogTime();
            if (!logTime.equals("")) {
                LocalDate startTime = DateUtils.parseByHour(logTime);
                LocalDate endTime = LocalDate.now();
                long until = startTime.until(endTime, ChronoUnit.DAYS); //比较相差多少天
                logTime = String.valueOf(until);
                personListVO.setLogTime(logTime);
            }
        }
        return new PageBean<>(list);
    }

    @Override
    public ArrayList<HashMap<String, String>> getManagerList() {
        ArrayList<HashMap<String, String>> result = new ArrayList<>();
        ArrayList<String> list = personMapper.getManagerList();
        for (String manager : list) {
            HashMap<String, String> map = new HashMap<>();
            map.put("manager", manager);
            result.add(map);
        }
        return result;
    }

    @Override
    public ArrayList<HashMap<String, String>> getQueueStatusList() {
        ArrayList<HashMap<String, String>> result = new ArrayList<>();
        ArrayList<String> list = personMapper.getQueueStatusList();
        for (String queueStatus : list) {
            HashMap<String, String> map = new HashMap<>();
            map.put("queueStatus", queueStatus);
            result.add(map);
        }
        return result;
    }

    @Override
    public ArrayList<HashMap<String, String>> getRunStatusList() {
        ArrayList<HashMap<String, String>> result = new ArrayList<>();
        ArrayList<String> list = personMapper.getRunStatusList();
        for (String runStatus : list) {
            HashMap<String, String> map = new HashMap<>();
            map.put("runStatus", runStatus);
            result.add(map);
        }
        return result;
    }

    @Override
    public ArrayList<HashMap<String, String>> getLogStatusList() {
        ArrayList<HashMap<String, String>> result = new ArrayList<>();
        ArrayList<String> list = personMapper.getLogStatusList();
        for (String logStatus : list) {
            HashMap<String, String> map = new HashMap<>();
            map.put("logStatus", logStatus);
            result.add(map);
        }
        return result;
    }

    @Override
    public PageBean<PersonConfigListVO> configListPage(Integer page, Integer limit, PersonConfigListQuery personConfigListQuery) {
        // 分页查询
        PageHelper.startPage(page, limit);
        List<PersonConfigListVO> list = personMapper.configListPage(personConfigListQuery);
        return new PageBean<>(list);
    }


    @Override
    public ArrayList<HashMap<String, String>> getConfigManagerList() {
        ArrayList<HashMap<String, String>> result = new ArrayList<>();
        ArrayList<String> list = personMapper.getConfigManagerList();
        for (String manager : list) {
            HashMap<String, String> map = new HashMap<>();
            map.put("manager", manager);
            result.add(map);
        }
        return result;
    }

    @Override
    public PersonConfigListVO getConfigById(Integer id) {
        return personMapper.getConfigById(id);
    }

    @Override
    public Boolean updateById(PersonConfigQuery personConfigQuery) {
        return personMapper.updateById(personConfigQuery);
    }

    @Override
    public Boolean addData(PersonConfigQuery personConfigQuery) {
        return personMapper.addData(personConfigQuery);
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void foreverRemove(Integer id, Integer showId) {
        int result = personMapper.foreverRemove(id); // 删除配置表数据
        int result2 = personMapper.foreverRemove2(showId); // 删除主表数据
//        if (result != 1 || result2 != 1) {
//            throw new BusinessException("删除失败");
//        }
    }

    @Override
    public Integer updateScheduledName(String scheduledName) {
        Integer scheduledId = ScheduleUtils.getScheduledId("192.168.1.181", scheduledName);
        if (scheduledId == 0) {
            throw new BusinessException("未找到该定时任务，请检查定时任务名称或服务器ip是否正确");
        }

        return scheduledId;
    }

    @Override
    public void updateRemarks(Integer id, String remarks) {
        int result = personMapper.updateRemarks(id, remarks);
        if (result != 1) {
            throw new BusinessException("修改失败");
        }
    }
}
